<template>
  <div class="app-container">
    <div class="header-bar" style="justify-content: flex-end;">
      <el-input
        placeholder="请输入合同/需求编号、名称"
        style="width: 240px; float: right; margin-bottom: 10px;"
        v-model="queryParams.keyWord"
        @keyup.enter="handleSearch"
        clearable
      >
        <template #append>
          <el-button icon="Search" @click="handleSearch"></el-button>
        </template>
      </el-input>
    </div>

    <el-table :loading="loading" :data="planningList" style="width: 100%" border>
      <el-table-column prop="dno" label="需求编号" align="left" :show-overflow-tooltip="true" />
      <el-table-column prop="dname" label="需求名称" align="left" :show-overflow-tooltip="true" />
      <el-table-column prop="cname" label="关联合同名称" align="left" :show-overflow-tooltip="true" />
      <el-table-column prop="cno" label="关联合同编号" align="left" :show-overflow-tooltip="true" />
      <el-table-column prop="materialName" label="物料" align="left" :show-overflow-tooltip="true" />
      <el-table-column prop="status" label="当前状态" align="left">
        <template #default="scope">
          <span>{{ getStatusLabel(scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="sales" label="销售人员" align="left" :show-overflow-tooltip="true" />
      <el-table-column prop="createTime" label="创建时间" align="left" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="left" width="180" fixed="right">
        <template #default="scope">
          <el-link type="primary" @click="handleView(scope.row)">查看</el-link>
          <template v-if="scope.row.status === '1'">
            <el-link type="primary" class="ml5" @click="handlePushProduction(scope.row)">推送排产</el-link>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      style="margin-top: 20px; justify-content: flex-end;"
      v-model:current-page="queryParams.pageNum"
      v-model:page-size="queryParams.pageSize"
      :page-sizes="[10, 20, 30, 50]"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 推送排产弹窗 -->
    <ProductionSchedulingPush
      v-model:visible="pushDialogVisible"
      :data="selectedRow"
      @close="pushDialogVisible = false"
      @confirm="handlePushConfirm"
    />
  </div>
</template>

<script setup name="planning">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
import ProductionSchedulingPush from './productionSchedulingPush.vue'
import {
  getSchemePage
} from '@/api/scheme'
const router = useRouter();
// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
})

/** 查询计划列表 */
const getSchemeList = () => {
  loading.value = true
  getSchemePage(queryParams).then(res => {
    loading.value = false
    planningList.value = res.data?.list || [];
    total.value = res.data?.total || 0;
  }).catch(() => {
    planningList.value = [];
    total.value = 0;
  });
}

// 数据总数
const total = ref(0) // 模拟数据总数

// 加载状态
const loading = ref(false)
// 推送排产弹窗可见状态
const pushDialogVisible = ref(false)
// 当前选中的行数据
const selectedRow = ref({})
// 状态选项
const statusOptions = [
  { label: '待计划', value: 1 },
  { label: '排产中', value: 2 },
  { label: '已完成', value: 3 }
]

// 表格数据
const planningList = ref([])

// 获取状态标签
function getStatusLabel(value) {
  const option = statusOptions.find(item => Number(item.value) === Number(value))
  return option ? option.label : value
}

// 初始化
onMounted(() => {
  getSchemeList()
})

// 搜索
function handleSearch() {
  queryParams.pageNum = 1
  getSchemeList()
}

// 查看计划
function handleView(row) {
  console.log('查看计划:', row)
  router.push({
    path: '/scheme/schemeDetails/index',
  })
  // 实际项目中这里会跳转到详情页
}

// 推送排产
function handlePushProduction(row) {
  selectedRow.value = { ...row }
  pushDialogVisible.value = true
}
// 推送排产确认
function handlePushConfirm() {
  console.log('确认推送排产:', selectedRow.value)
  // ElMessageBox.confirm('确定要推送排产该计划吗？', '提示', {
  //   confirmButtonText: '确定',
  //   cancelButtonText: '取消',
  //   type: 'warning'
  // }).then(() => {
  //   // 调用推送排产接口
  //   console.log('111', row);
  // }).catch(() => {
  //   // 取消删除
  // })
  pushDialogVisible.value = false
}

// 分页大小改变
function handleSizeChange(val) {
  queryParams.pageSize = val
  queryParams.pageNum = 1
  getSchemeList()
}

// 当前页码改变
function handleCurrentChange(val) {
  queryParams.pageNum = val
  getSchemeList()
}
</script>

<style scoped>
.app-container {
  padding: 20px;
  box-sizing: border-box;
}

.header-bar {
  margin-bottom: 10px;
  overflow: hidden;
}

.ml5 {
  margin-left: 15px;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
  box-sizing: border-box;
  width: 100%;
}

:deep(.el-pagination) {
  display: flex;
  justify-content: flex-end;
  flex-wrap: wrap;
}
</style>